<?php
// +----------------------------------------------------------------------
// | Fanwe 方维众筹商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 甘味人生(526130@qq.com)
// +----------------------------------------------------------------------

class BmUserMarginAction extends CommonAction
{
    public function index()
    {
		$bm_config = load_auto_cache("bm_config");
		
        $login_name=strim($_REQUEST['login_name']);
        $login_name1=strim($_REQUEST['login_name1']);

        /*$time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}
		
        $where = " bpg.create_time between " . $time['begin_time'] . " and " . $time['end_time'] . "";*/
     	$where = " u.is_effect = 1  and bp.member_id = 0 ";
		$where .= " and bp.pid != 0 and bp.type = 2";
		if($login_name){
     		$where .=" and bp.login_name = '".$login_name."' ";
     	}elseif ($login_name1){
     		$where .= " and bp.login_name like '%" . $login_name1 . "%' ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "u.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
		 
        $sql_count = "SELECT 
						u.id,
						bp.name as nick_name,
						u.coin,
						bp.login_name,
						if(sum(bpg.gain),sum(bpg.gain),0) as gain,
						u.bm_pid,bp.pid  
					FROM   
					". $pre . "user AS u 
					LEFT JOIN ". $pre . "bm_promoter_game_log as bpg on bpg.bm_pid = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.id = bp.user_id 
					WHERE 
						$where 
					GROUP BY u.id 
					order by ".$order." ".$sort;
					
		//$list_count_row=$GLOBALS['db']->getRow($sql_count);
		
		$p = $_REQUEST['p'];
		if ($p == '') {
			$p = 1;
		}
		$p         = $p > 0 ? $p : 1;
		$page_size = 10;
		$limit     = (($p - 1) * $page_size) . "," . $page_size;

		$count = $GLOBALS['db']->getOne("SELECT count(1) FROM ({$sql_count}) AS a");

        if($count > 0){

			$page      = new Page($count, $page_size);
			$page_show = $page->show();
	
			$sql = "SELECT 
						u.id,
						bp.name as nick_name,
						u.coin,
						bp.login_name,
						if(sum(bpg.gain),sum(bpg.gain),0) as gain,
						u.bm_pid,bp.pid  
					FROM   
					". $pre . "user AS u 
					LEFT JOIN ". $pre . "bm_promoter_game_log as bpg on bpg.bm_pid = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.id = bp.user_id 
					WHERE 
						$where 
					GROUP BY u.id 
					order by ".$order." ".$sort." 
					limit ".$limit;

			$list = $GLOBALS['db']->getAll($sql);
			
			foreach($list as $k=>$v)
            {
            	if($v['id']){
            		//鱼乐合伙人编号
            		$list[$k]['bm_login_name'] = $GLOBALS['db']->getOne("select login_name from ". $pre . "bm_promoter where user_id=".$v['pid']);
            		
            	}
            	
            	//保证金
            	$list[$k]['promoter_deposit'] = $bm_config['promoter_deposit'];
            	//可提现
				$list[$k]['refund_coin'] = $v['coin'] - $bm_config['promoter_deposit'];
				if($list[$k]['refund_coin'] < 0)
				{
					$list[$k]['refund_coin'] = 0 ;
				}
            }
		}
		
		
		$sortImg = $sort; //排序图标
		$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
		$sort = $sort == 'desc' ? 1 : 0; //排序方式
		//模板赋值显示
		
		$this->assign ( 'sort', $sort );
		$this->assign ( 'order', $order );
		$this->assign ( 'sortImg', $sortImg );
		$this->assign ( 'sortType', $sortAlt );
		
		
        $this->assign('list', $list);
        $this->assign('page', $page_show);
		$this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
		$this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
		$this->assign('login_name', $login_name);

		$bm_config = load_auto_cache("bm_config");
		$this->assign('bm_one',$bm_config['bm_one']); 
		$this->assign('bm_two',$bm_config['bm_two']); 
		
        $this->display();
    }
	//导出电子表
    public function export_csv($page = 1)
    {

        $pagesize = 10;
        set_time_limit(0);
        $limit = (($page - 1)*intval($pagesize)).",".(intval($pagesize));
		
		$date = $_REQUEST;
		
		/*********************/
		
		/*------------------------*/
		$bm_config = load_auto_cache("bm_config");
		
        $login_name=strim($_REQUEST['login_name']);
        $login_name1=strim($_REQUEST['login_name1']);

        /*$time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}
		
        $where = " bpg.create_time between " . $time['begin_time'] . " and " . $time['end_time'] . "";*/
     	$where = " u.is_effect = 1  and bp.member_id = 0 ";
		$where .= " and bp.pid != 0 and bp.type = 2";
		if($login_name){
     		$where .=" and bp.login_name = '".$login_name."' ";
     	}elseif ($login_name1){
     		$where .= " and bp.login_name like '%" . $login_name1 . "%' ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "u.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
		 
        $sql_count = "SELECT 
						u.id,
						bp.name as nick_name,
						u.coin,
						bp.login_name,
						if(sum(bpg.gain),sum(bpg.gain),0) as gain,
						u.bm_pid,bp.pid  
					FROM   
					". $pre . "user AS u 
					LEFT JOIN ". $pre . "bm_promoter_game_log as bpg on bpg.bm_pid = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.id = bp.user_id 
					WHERE 
						$where 
					GROUP BY u.id 
					order by ".$order." ".$sort;
					
		//$list_count_row=$GLOBALS['db']->getRow($sql_count);

		$count = $GLOBALS['db']->getOne("SELECT count(1) FROM ({$sql_count}) AS a");

        if($count > 0){
	
			$sql = "SELECT 
						u.id,
						bp.name as nick_name,
						u.coin,
						bp.login_name,
						if(sum(bpg.gain),sum(bpg.gain),0) as gain,
						u.bm_pid,bp.pid  
					FROM   
					". $pre . "user AS u 
					LEFT JOIN ". $pre . "bm_promoter_game_log as bpg on bpg.bm_pid = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.id = bp.user_id 
					WHERE 
						$where 
					GROUP BY u.id 
					order by ".$order." ".$sort." 
					limit ".$limit;

			$list = $GLOBALS['db']->getAll($sql);
			
			foreach($list as $k=>$v)
            {
            	if($v['id']){
            		//鱼乐合伙人编号
            		$list[$k]['bm_login_name'] = $GLOBALS['db']->getOne("select login_name from ". $pre . "bm_promoter where user_id=".$v['pid']);
            		
            	}
            	
            	//保证金
            	$list[$k]['promoter_deposit'] = $bm_config['promoter_deposit'];
            	//可提现
				$list[$k]['refund_coin'] = $v['coin'] - $bm_config['promoter_deposit'];
				if($list[$k]['refund_coin'] < 0)
				{
					$list[$k]['refund_coin'] = 0 ;
				}
            }
		}
		else
		{
			if($page==1)
				$this->error(L("NO_RESULT"));
		}

		/*------------------------*/
		/*********************/
		$voList = $list;

        if($voList)
        {
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示
	
			
			$bm_config = load_auto_cache("bm_config");
			
            register_shutdown_function(array(&$this, 'export_csv'), $page+1);

            $refund_value = array( 'login_name'=>'""', 'nick_name'=>'""', 'promoter_deposit'=>'""','gain'=>'""','coin'=>'""','refund_coin'=>'""','bm_login_name'=>'""');
            if($page == 1)
            {
                $content = iconv("utf-8","gbk","编号,".$bm_config['bm_two']."名称,期初保证金,当前盈亏留存,当前保证金,可提现保证金,所属".$bm_config['bm_two']."编号");
                $content = $content . "\n";
            }

 
            foreach($voList as $k=>$v)
            {
				$refund_value['login_name'] = '"' . iconv('utf-8','gbk',$v['login_name']) . '"'."\t";
				$refund_value['nick_name'] = '"' . iconv('utf-8','gbk',$v['nick_name']) . '"'."\t";
				$refund_value['promoter_deposit'] = '"' . iconv('utf-8','gbk',$v['promoter_deposit']) . '"'."\t";
				$refund_value['gain'] = '"' . iconv('utf-8','gbk',$v['gain']) . '"'."\t";
				$refund_value['coin'] = '"' . iconv('utf-8','gbk',$v['coin']) . '"'."\t";
				$refund_value['refund_coin'] = '"' . iconv('utf-8','gbk',$v['refund_coin']) . '"'."\t";
				$refund_value['bm_login_name'] = '"' . iconv('utf-8','gbk',$v['bm_login_name']) . '"'."\t";
				
                $content .= implode(",", $refund_value) . "\n";
            }

            //
            header("Content-Disposition: attachment; filename=refund_list.csv");

            echo $content ;
        }
        else
        {
            if($page==1)
                $this->error(L("NO_RESULT"));
        }

    }
}
